﻿@using System.Globalization;
@using System.Web.WebPages.Scope;

@functions {  
    private const string InitializedExceptionMessage = "The Disqus Helper has not been initialized.";
    private const string ArgumentNullExceptionMessage = "Argument cannot be null or empty.";
    
    private static readonly object _apiKey = new object();
    private static readonly object _initializedKey = new object();

    public static string ForumShortName {
		get {
			if (!Initialized) {
				throw new InvalidOperationException(InitializedExceptionMessage);
			}
			return (string)(ScopeStorage.CurrentScope[_apiKey] ?? "");
		}

		private set {
			if (value == null) {
				throw new ArgumentNullException("ApiKey");
			}

			ScopeStorage.CurrentScope[_apiKey] = value;
		}
	}
    
    private static bool Initialized {
		get {
			return (bool)(ScopeStorage.CurrentScope[_initializedKey] ?? false);
		}
		set {
			ScopeStorage.CurrentScope[_initializedKey] = value;
		}
	}
    
    /// <summary>
    /// Initializes the Disqus helper.
    /// </summary>
    /// <param name="apiKey">The site shortname.</param>
	public static void Initialize(string forumShortName) {
		 if (forumShortName.IsEmpty()) {
                throw new ArgumentException(ArgumentNullExceptionMessage, "forumShortName");
         }

		ForumShortName = forumShortName;

		Initialized = true;
	}
    
}

@helper ShowComments(string pageIdentifier){
    <p> 
      <div id="disqus_thread"></div> 
    </p> 
    <script type='text/javascript'> 
        var disqus_shortname = '@ForumShortName';
        var disqus_developer = 1;        
        var disqus_identifier = '@pageIdentifier';        
        
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();   
    </script> 
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
}